package com.hans.scala.actor

import scala.actors.Actor

/**
 * Created by Juntao.Zhang on 2014/5/20.
 */
object NameResolver extends Actor {
  import java.net.{InetAddress, UnknownHostException}

  def act() {
    react {
      case (name: String, actor: Actor) =>
        actor ! getIp(name)
        act()
      case "EXIT" =>
        println("Name resolver exiting.")
      // quit
      case msg =>
        println("Unhandled message: "+ msg)
        act()
    }
  }

  def getIp(name: String): Option[InetAddress] = {

    try {
      Some(InetAddress.getByName(name))
    } catch {
      case _:UnknownHostException => None
    }

  }
}
